package com.freedom.admin.interfaces.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freedom.admin.application.service.cmd.UserCmdService;
import com.freedom.admin.application.service.query.UserQueryService;
import com.freedom.admin.infrastructure.persistence.pojo.form.UserForm;
import com.freedom.admin.infrastructure.persistence.pojo.form.UserPasswordForm;
import com.freedom.admin.infrastructure.persistence.pojo.form.UserStatusForm;
import com.freedom.admin.infrastructure.persistence.pojo.query.UserPageQuery;
import com.freedom.admin.infrastructure.persistence.pojo.vo.user.LoginUserVO;
import com.freedom.admin.infrastructure.persistence.pojo.vo.user.UserVO;
import com.freedom.common.core.base.BaseIntsForm;
import com.freedom.common.core.result.CommonResult;
import com.freedom.common.core.result.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "「管理端」用户管理")
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class SysUserController {

    private final UserCmdService userCmdService;

    private final UserQueryService userQueryService;

    @ApiOperation(value = "用户分页列表")
    @GetMapping("/pages")
    public PageResult<UserVO> listUserPages(@Validated UserPageQuery queryParams) {
        Page<UserVO> result = userQueryService.listUserPages(queryParams);
        return PageResult.success(result);
    }

    @ApiOperation(value = "用户表单数据")
    @GetMapping("/{userId}/form_data")
    public CommonResult<UserForm> findUserFormData(@ApiParam(value = "用户ID") @PathVariable Integer userId) {
        UserForm userForm = userQueryService.findUserFormData(userId);
        return CommonResult.success(userForm);
    }

    @ApiOperation(value = "新增用户")
    @PostMapping
    public CommonResult<Boolean> addUser(@RequestBody @Validated UserForm userForm) {
        return CommonResult.judge(userCmdService.addUser(userForm));
    }

    @ApiOperation(value = "修改用户")
    @PutMapping(value = "/edit")
    public CommonResult<Boolean> modifyUser(@RequestBody @Validated UserForm userForm) {
        boolean result = userCmdService.modifyUser(userForm);
        return CommonResult.judge(result);
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/delete")
    public CommonResult<Boolean> removeUsers(@RequestBody @Validated BaseIntsForm baseIntsForm) {
        boolean result = userCmdService.removeUsers(baseIntsForm.getIds());
        return CommonResult.judge(result);
    }

    @ApiOperation(value = "修改用户密码")
    @PutMapping(value = "/password")
    public CommonResult<Boolean> modifyUserPassword(@RequestBody @Validated UserPasswordForm userPasswordForm) {
        boolean result = userCmdService.modifyUserPassword(userPasswordForm);
        return CommonResult.judge(result);
    }

    @ApiOperation(value = "修改用户状态")
    @PutMapping(value = "/status")
    public CommonResult<Boolean> modifyUserStatus(@RequestBody @Validated UserStatusForm userStatusForm) {
        boolean result = userCmdService.modifyUserStatus(userStatusForm);
        return CommonResult.judge(result);
    }

    @ApiOperation(value = "获取登录用户信息")
    @GetMapping("/me")
    public CommonResult<LoginUserVO> getLoginUserInfo() {
        LoginUserVO loginUserVO = userQueryService.getLoginUserInfo();
        return CommonResult.success(loginUserVO);
    }

}
